package org.dromara.solonplugins.kafka1;

import java.util.concurrent.Future;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.noear.solon.lang.Nullable;

public class Kafka1ProducerTemplate {
	
	private final KafkaProducer<String, String> producer;
	private String defaultTopic;
	
	public Kafka1ProducerTemplate(KafkaProducer<String, String> producer) {
		this.producer = producer;
	}
	
	public Future<RecordMetadata> send(String topic, @Nullable String data) {
		return producer.send(new ProducerRecord<>(topic, data));
	}
	
	/**
	 * The default topic for send methods where a topic is not
	 * provided.
	 * @return the topic.
	 */
	public String getDefaultTopic() {
		return this.defaultTopic;
	}

	/**
	 * Set the default topic for send methods where a topic is not
	 * provided.
	 * @param defaultTopic the topic.
	 */
	public void setDefaultTopic(String defaultTopic) {
		this.defaultTopic = defaultTopic;
	}
	
	protected void stop() {
		if (producer != null) {
			producer.close();// 关闭生产者
		}
	}

}
